package com.example.spider.web;

import com.example.spider.provider.m3u8.task.M3u8MetaInfoParserTask;
import com.example.spider.provider.speedTest.SpeedTestListHandler;
import com.example.spider.task.SpiderTaskQueue;
import com.example.spider.util.HtmlUtil;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 触发 http://localhost:8080/index.html
 * 触发 http://localhost:8080/m3u8/
 * 触发 http://localhost:8080/m3u8/test
 */
@Validated
@RequestMapping("m3u8")
@RestController
public class M3u8Controller {

    @RequestMapping(value = "create")
    public String createNew(@NotEmpty String url, @NotEmpty String id, @NotEmpty String name) throws IOException {

        M3u8MetaInfoParserTask t = new M3u8MetaInfoParserTask()
                .setId(id)
                .setName(name.trim())
                .setTitle(name.trim())
                .setSource(HtmlUtil.parseDomainFromUrl(url))
                .setUrl(url.trim());
        SpiderTaskQueue.putTask(t);
        return "ok";
    }

    @RequestMapping(value = "test")
    public String createNew(@NotEmpty String url) throws IOException {
        SpeedTestListHandler.test(url);
        return "ok";
    }

}
